﻿using System.IO;
using IndicoInterface.SimpleAgendaDataModel;

namespace IndicoAutoDownloader
{
    static class IndicoUtils
    {
        /// <summary>
        /// Create a new sub-dir for a session.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="currentDir"></param>
        /// <returns></returns>
        public static DirectoryInfo DirectoryName(this Session s, DirectoryInfo currentDir)
        {
            var date = s.StartDate.ToLongDateString();
            var name = string.Format("{0} - {1}", s.ID, s.Title).Clean();
            if (name.Length > 30)
            {
                var hs = name.GetHashCode();
                name = string.Format("{0} - {1}", name.Substring(0, 30), hs);
            }
            return new DirectoryInfo(Path.Combine(currentDir.FullName, date, name));
        }

        /// <summary>
        /// Make safe for file and directory names.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Clean(this string s)
        {
            return s.Replace(":", "")
                .Replace("\\", "")
                .Replace("/", "")
                .Replace("*", " ")
                .Replace("?", "")
                .Replace("\"", "'")
                .Replace("\"", "")
                .Replace("\"", "")
                .Replace("<", "")
                .Replace(">", "")
                .Replace("|", " ");
        }

        /// <summary>
        /// Returns a name prefix for a talk.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string NamePrefix(this Talk t)
        {
            var startTime = t.StartDate.ToString("HH:mm");
            string speakers = "";
            if (t.Speakers != null)
            {
                foreach (var s in t.Speakers)
                {
                    if (!string.IsNullOrWhiteSpace(speakers))
                        speakers += ", ";
                    speakers += s;
                }
            }
            if (string.IsNullOrWhiteSpace(speakers))
                speakers = "Unknown";
            return string.Format("{0} - {1} - {2}", startTime, speakers, t.Title).Clean();
        }
    }
}
